package com.it.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.it.blog.domain.Article;
import com.it.blog.domain.ArticleType;
import com.it.blog.dto.PlaceOnFileDTO;
import com.it.blog.mapper.ArticleMapper;
import com.it.blog.mapper.ArticleTypeMapper;
import com.it.blog.query.ArticleQuery;
import com.it.blog.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service//
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Override
    public List<Article> findAll() {
        return articleMapper.findAll();
    }

    @Override
    public Article findOne(Long id) {
        return articleMapper.findOne(id);
    }

    @Override
    public List<Article> queryPage(ArticleQuery articleQuery) {
        PageHelper.startPage(articleQuery.getCurrentPage(), articleQuery.getPageSize());

        return articleMapper.queryPage(articleQuery);
    }

    @Override
    public void addArticle(Article article) {
        articleMapper.addArticle(article);
    }

    @Override
    public void updateArticle(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.updateArticle(article);
    }

    //alt shif p
    @Override
    public void deleteArticle(Long id) {
        articleMapper.deleteArticle(id);
    }

    @Override
    public void batchArticles(Long[] ids) {
        articleMapper.batchArticles(ids);
    }

    @Override
    public List<ArticleType> getTypeTree(Long id) {
        List<ArticleType> articleTypeList = articleTypeMapper.findAll();
        Map<Long, ArticleType> map = new HashMap<>();
        for (ArticleType articleType : articleTypeList) {
            Long Id = articleType.getId();
            map.put(Id, articleType);
        }
        List<ArticleType> treeList= new ArrayList<>();
        for (ArticleType articleType : articleTypeList) {
            //编辑时，不能选自己为父类型
            if (id.equals(articleType.getId())) {
                continue;
            }
            Long parentId = articleType.getParentId();
            if (parentId == null) {
                treeList.add(articleType);
            } else {
                ArticleType articleTypeParent = map.get(parentId);
                articleTypeParent.getChildren().add(articleType);
            }
        }
        return treeList;
    }

    @Override
    public List<PlaceOnFileDTO> getArticleTimeStatistics() {
        return articleMapper.getArticleTimeStatistics();
    }

    @Override
    public List<Article> getRelatedArticles(Long id) {
        Article article = articleMapper.findOne(id);
        return articleMapper.getRelatedArticles(article);
    }

    @Override
    public List<Article> getArchiveDetail(String yearMonth) {
        return articleMapper.getArchiveDetail(yearMonth);
    }

    @Override
    public List<Article> findAllArticle() {
        return articleMapper.findAllArticle();
    }
}
